package com.lighthouse.doc.base.controller;

import com.lighthouse.common.enums.DocStatus;
import com.lighthouse.doc.base.domain.KnowledgeDocBaseInfo;
import com.lighthouse.doc.base.domain.KnowledgeDocBaseInfoVo;
import com.lighthouse.doc.base.domain.SearchInfo;
import com.lighthouse.doc.base.service.IKnowledgeDocBaseInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 文档信息Controller
 * 
 * @author ysz
 * @date 2023-03-22
 */
@RestController
@RequestMapping("/knowledge/doc")
public class KnowledgeDocController extends BaseController
{
    @Autowired
    private IKnowledgeDocBaseInfoService knowledgeDocBaseInfoService;


    /**
     * 查询文档信息列表 
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:list')")
    @GetMapping("/list")
    public TableDataInfo list(KnowledgeDocBaseInfo knowledgeDocBaseInfo) throws Exception {
        startPage();
        List<KnowledgeDocBaseInfo> list = knowledgeDocBaseInfoService.selectKnowledgeDocBaseInfoList(knowledgeDocBaseInfo);
        return getDataTable(list);
    }

    /**
     * 全局检索，查询列表
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:list')")
    @GetMapping(value = "/search")
    public TableDataInfo search(SearchInfo searchInfo) throws IOException {
        List<KnowledgeDocBaseInfoVo> knowledgeDocBaseInfos = knowledgeDocBaseInfoService.searchKnowledgeDocBaseInfoList(searchInfo);
        return getDataTable(knowledgeDocBaseInfos);
    }

    /**
     * 导出文档信息列表
     */
//    @PreAuthorize("@ss.hasPermi('knowledge:doc:export')")
//    @Log(title = "文档信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, KnowledgeDocBaseInfo knowledgeDocBaseInfo)
//    {
//        List<KnowledgeDocBaseInfo> list = knowledgeDocBaseInfoService.selectKnowledgeDocBaseInfoList(knowledgeDocBaseInfo);
//        ExcelUtil<KnowledgeDocBaseInfo> util = new ExcelUtil<KnowledgeDocBaseInfo>(KnowledgeDocBaseInfo.class);
//        util.exportExcel(response, list, "文档信息数据");
//    }

    /**
     * 获取文档信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:query')")
    @GetMapping(value = "/{docId}")
    public AjaxResult getInfo(@PathVariable("docId") String docId)
    {
        return toAjax(knowledgeDocBaseInfoService.selectKnowledgeDocBaseInfoByDocId(docId));
    }

    /**
     * 新增文档信息
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:add')")
    @Log(title = "文档信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgeDocBaseInfo knowledgeDocBaseInfo)
    {
        return toAjax(knowledgeDocBaseInfoService.insertKnowledgeDocBaseInfo(knowledgeDocBaseInfo));
    }

    /**
     * 修改文档信息
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:edit')")
    @Log(title = "文档信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowledgeDocBaseInfo knowledgeDocBaseInfo)
    {
        return toAjax(knowledgeDocBaseInfoService.updateKnowledgeDocBaseInfo(knowledgeDocBaseInfo));
    }

    /**
     * 删除文档信息
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:remove')")
    @Log(title = "文档信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{docIds}")
    public AjaxResult remove(@PathVariable String[] docIds)
    {
        return toAjax(knowledgeDocBaseInfoService.deleteKnowledgeDocBaseInfoByDocIds(docIds));
    }

    /**
     * 查询最近关注文档
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:list')")
    @GetMapping("/recent/attention/{attentionType}")
    public TableDataInfo recentAttention(@PathVariable("attentionType") String attentionType) throws Exception {
        startPage();
        List<KnowledgeDocBaseInfo> list;
        if ("browse".equals(attentionType)) {
            // 最近浏览
            list = knowledgeDocBaseInfoService.selectKnowledgeDocRecentBrowse(SecurityUtils.getUserId());
        } else if ("published".equals(attentionType)) {
            // 最近发布
            list = knowledgeDocBaseInfoService.selectKnowledgeDocByStatus(DocStatus.PUBLISHED.getCode(), SecurityUtils.getUserId());
        }  else if ("draft".equals(attentionType)) {
            // 待发布
            list = knowledgeDocBaseInfoService.selectKnowledgeDocByStatus(DocStatus.DRAFT.getCode(), SecurityUtils.getUserId());
        } else {
            // 最近关注
            list = knowledgeDocBaseInfoService.selectKnowledgeDocRecentAttention(SecurityUtils.getUserId(), attentionType);
        }
        return getDataTable(list);
    }

    /**
     * 热榜
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:list')")
    @GetMapping("/hot")
    public TableDataInfo hotDoc(KnowledgeDocBaseInfo knowledgeDocBaseInfo) throws Exception {
        List<KnowledgeDocBaseInfo> list = knowledgeDocBaseInfoService.selectKnowledgeDocHot();
        return getDataTable(list);
    }

    /**
     * 推荐
     */
    @PreAuthorize("@ss.hasPermi('knowledge:doc:list')")
    @GetMapping("/suggest")
    public TableDataInfo suggestDoc(KnowledgeDocBaseInfo knowledgeDocBaseInfo) throws Exception {
        List<KnowledgeDocBaseInfo> list = knowledgeDocBaseInfoService.selectKnowledgeDocSuggest();
        return getDataTable(list);
    }
}
